package com.sg.service.base.operation.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.dto.FieldInfo;
import com.sg.common.exception.BizException;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.operation.req.QueryDataSeparationListReq;
import com.sg.entity.OmsPlatformOperationSeparatedData;
import com.sg.mapper.OmsPlatformOperationSeparatedDataMapper;
import com.sg.service.base.operation.MOmsPlatformOperationSeparatedDataService;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsPlatformOperationSeparatedDataServiceImpl
        extends ServiceImpl<OmsPlatformOperationSeparatedDataMapper, OmsPlatformOperationSeparatedData>
        implements MOmsPlatformOperationSeparatedDataService {

    @Autowired
    OmsPlatformOperationSeparatedDataMapper omsPlatformOperationSeparatedDataMapper;

    /**
     * 6-1-02修改数据分离[6928]
     * gen by moon at 5/22/2023, 9:10:50 PM
     */
    @Trace(operationName = "6-1-02修改数据分离")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateDataSeparation(OmsPlatformOperationSeparatedData omsPlatformOperationSeparatedData) {

        return super.updateById(omsPlatformOperationSeparatedData);
    }

    /**
     * 6-1-02查询数据分离列表[6929]
     * gen by moon at 5/22/2023, 9:10:53 PM
     */
    @Trace(operationName = "6-1-02查询数据分离列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsPlatformOperationSeparatedData> queryDataSeparationList(QueryDataSeparationListReq reqDto) {

        try {
            QueryWrapper<OmsPlatformOperationSeparatedData> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsPlatformOperationSeparatedData.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsPlatformOperationSeparatedData.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsPlatformOperationSeparatedData.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsPlatformOperationSeparatedData>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 6-1-02查询数据分离详情[6930]
     * gen by moon at 5/22/2023, 9:10:53 PM
     */
    @Trace(operationName = "6-1-02查询数据分离详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsPlatformOperationSeparatedData queryDataSeparationDetail(OmsPlatformOperationSeparatedData omsPlatformOperationSeparatedData) {

        try {

            QueryWrapper<OmsPlatformOperationSeparatedData> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(omsPlatformOperationSeparatedData);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsPlatformOperationSeparatedData> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsPlatformOperationSeparatedData retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M6修改租户分离数据[7206]
     * gen by moon at 9/13/2023, 10:40:11 PM
     */
    @Trace(operationName = "M6修改租户分离数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTenantSeparationData(OmsPlatformOperationSeparatedData omsPlatformOperationSeparatedData) {
        omsPlatformOperationSeparatedData.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsPlatformOperationSeparatedData.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsPlatformOperationSeparatedData);
    }
}
